/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference path="../Items/MItem.js" />
/// <reference path="../Root.js" />
/// <reference name="MicrosoftAjax.debug.js" />


Type.registerNamespace("BM");

BM.RootValidator = function(root) {
    ///	<summary locid="T:J#BM.RootValidator">
    ///	This class is responsible for validating the MForm tree and for keeping information about which controls 
    /// are not valid    
    ///	</summary>
    ///	<param name="root" type="MForm">
    ///	the root of the MForm tree
    ///	</param>    
    /// <field name="root" Type="MForm">
    /// the root of the MForm tree
    /// </field>    
    ///	<returns type="BM.RootValidator" />
    BM.RootValidator.initializeBase(this, []);
    this.root = root;
    this.itemsWithError = new Object();
}

BM.RootValidator.prototype = {
    itemRemoved: function(sender, eventArgs) {
        this.notifyAboutSuccess(sender);
    },

    notifyAboutError: function(validationError) {
        if (!this.itemsWithError[validationError.item.getId()]) {
            validationError.item.addUnloading(Function.createDelegate(this, this.itemRemoved));
        }        
        this.itemsWithError[validationError.item.getId()] = validationError;
        this.updateValidationText();
    },

    notifyAboutSuccess: function(item) {
        item.removeUnloading(Function.createDelegate(this, this.itemRemoved));
        var id = item.getId();
        if (this.itemsWithError[id]) {
            delete this.itemsWithError[id];
        }
        this.updateValidationText();
    },

    getValidationSpan: function() {
        return $get(this.root._element.id + '_Validator');
    },

    updateValidationText: function() {
        var errorsArray = new Array();
        for (var key in this.itemsWithError) {
            var validationError = this.itemsWithError[key];
            if (validationError.item.isInUse()) {
                errorsArray.push(validationError.getCompleteErrorText());
            }
        }

        this.setValidationText(errorsArray.join("<br />"));
    },

    focusFirst: function() {
        for (var key in this.itemsWithError) {
            var validationError = this.itemsWithError[key];
            if (validationError.item.isInUse()) {
                validationError.item.focus();
                return;
            }
        }
    },

    setValidationText: function(text) {
        /// <summary>
        /// sets the text in the validation span
        /// </summary>
        /// <param name="text" type="String" mayBeNull="true">
        var validationSpan = this.getValidationSpan();
        if (validationSpan) {
            validationSpan.style.visibility = 'visible';
            validationSpan.errormessage = text;
            validationSpan.innerHTML = text;
        }
    },

    clearValidationText: function() {
        /// <summary>
        /// clears the text in the validation span
        /// </summary>
        this.setValidationText('');
    }
}

BM.RootValidator.registerClass("BM.RootValidator", Sys.Component);

BM.RootValidator.run = function(source, arguments) {
    var root = MItem.getById(source.rootId);
    var validationResult = root.validate();
    var validator = root.getMForm().getValidator();
    validator.focusFirst();
    return validationResult;
}


